home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d887.lha / NBuff / NBuffDemo.c < prev    next >
C/C++ Source or Header  |  1993-07-16  |  7KB  |  224 lines

  1. /***************************************************************************
  2. *                                                                          *
  3. *              DEMO CODE --- link with NBUFF.c                             *
  4. *                                                                          *
  5. ***************************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <intuition/intuitionbase.h>
  9. #include <graphics/gfxbase.h>
  10. #include <graphics/display.h>
  11.  
  12. #include <proto/exec.h>
  13. #include <proto/graphics.h>
  14. #include <proto/layers.h>
  15. #include <proto/intuition.h>
  16. #include <proto/dos.h>  // for Delay()
  17.  
  18. #include "NBuff.h"
  19.  
  20. #define DEPTH 2   // number of bitplanes per buffer
  21.  
  22. void main(void);
  23. int make_screen(SHORT, SHORT, SHORT, SHORT, UBYTE, UBYTE, USHORT, char *);
  24. int make_window(SHORT, SHORT, SHORT, SHORT, char *, UBYTE, UBYTE, ULONG,
  25.                 ULONG, struct Screen *, struct Gadget *);
  26. void DrawFigure(struct RastPort *, short, short);
  27.  
  28. /* Global Amiga system variables */
  29. // (already automatically present as externs with SAS C 5.10b)
  30. // struct IntuitionBase   *IntuitionBase = NULL;
  31. // struct GfxBase         *GfxBase       = NULL;
  32.  
  33. // NOTE:  Uncomment the following line if compiling under SAS C 5.10b.
  34. //        It's not needed in SAS C 6.2 (it's declared in <proto/layers.h>)
  35. // struct LayersBase      *LayersBase    = NULL;
  36.  
  37.  
  38. void main()
  39. {
  40.    /* Amiga system variables */
  41.    struct Screen   *screen    = NULL;  /* Intuition screen */
  42.    struct Window   *window    = NULL;  /* Intuition window */
  43.    struct RastPort *RP[2] =    { NULL, /* Intuition supplied rastport */
  44.                                  NULL };
  45.    short  j;
  46.  
  47.    /********************** Open Libraries ************************/
  48.    if (!(IntuitionBase = (struct IntuitionBase *)
  49.          OpenLibrary("intuition.library", 0)))
  50.    {
  51.         puts("NO INTUITION LIBRARY.");
  52.         goto CRAPOUT;
  53.    }
  54.  
  55.    if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0)))
  56.    {
  57.         puts("NO GRAPHICS LIBRARY.");
  58.         goto CRAPOUT;
  59.    }
  60.  
  61.    if ((LayersBase = OpenLibrary("layers.library", 0)) == NULL) {
  62.         puts("No layers library.");
  63.         goto CRAPOUT;
  64.         }
  65.  
  66.    /************* Open Intuition screen and window ********************/
  67.    if((screen = (struct Screen *)make_screen(0,640,400,DEPTH,0xff,0xff,
  68.                 HIRES|INTERLACE,NULL)) == NULL)
  69.    {
  70.       puts("Can't open screen.");
  71.       goto CRAPOUT;
  72.    }
  73.    ShowTitle(screen, FALSE);  // delete screen title for backdrop window
  74.  
  75.    /* get a window on the workbench     */
  76.    if((window = (struct Window *)make_window(0,0,640,400,NULL,0xff,0xff,
  77.                  BORDERLESS|SIMPLE_REFRESH|BACKDROP|ACTIVATE|NOCAREREFRESH,0,
  78.                  screen,NULL)) == NULL)
  79.    {
  80.       puts("Can't open window.");
  81.       goto CRAPOUT;
  82.    }
  83.  
  84.    RP[0] = window->RPort;
  85.    SetRast(RP[0], 2);
  86.            InitNBuff(screen,DEPTH,window,0);  // initialize buffer 0
  87.    RP[1] = InitNBuff(screen,DEPTH,window,1);  // initialize buffer 1
  88.  
  89.    /********** Loop which draws into alternate bitmaps and swaps view *********/
  90.    for(j=200; j<440; j++)
  91.    {
  92.       if(j%2)  /* Odd cases --- Draw to RP1 */
  93.       {
  94.          ShowView(1);
  95.          if(j-2) DrawFigure(RP[0],j-2,2);      /* Clear previous version */
  96.          DrawFigure(RP[0],j,1);                /* Draw this version      */
  97.       }
  98.       else     /* Even cases --- Draw to RP2 */
  99.       {
  100.          ShowView(0);
  101.          if(j-2 >= 0) DrawFigure(RP[1],j-2,0); /* Clear previous version */
  102.          DrawFigure(RP[1],j,1);                /* Draw this version      */
  103.       }
  104. //      Delay(1);
  105.    }
  106.    
  107. #ifdef FASTNBUFF
  108. puts("\nWhat you just saw was fast, Intuition-unfriendly double-buffered\nanimation, rapidly switching between 2 views having different\nbackground colors.\n");
  109. #else
  110. puts("\nWhat you just saw was double-buffered animation, rapidly switching\nbetween two views having different background colors.\n");
  111. #endif
  112.  
  113. CRAPOUT:
  114. if (RP[1]) FreeNBuff(screen, DEPTH, RP[1], 1);  // deallocate extra buffer
  115. if (RP[0]) FreeNBuff(screen, DEPTH, RP[0], 0);  // needed for general cleanup
  116.  
  117. /* Close stuff */
  118. if(window)        CloseWindow(window);
  119. if(screen)        CloseScreen(screen);
  120. if(LayersBase)    CloseLibrary(LayersBase);
  121. if(GfxBase)       CloseLibrary(GfxBase);
  122. if(IntuitionBase) CloseLibrary(IntuitionBase);
  123. }
  124.       
  125. /***********************************************************************/
  126.  
  127. int make_screen(SHORT y, SHORT w, SHORT h, SHORT d,
  128.             UBYTE colour0, UBYTE colour1,
  129.             USHORT mode, char *name)
  130. {
  131.    struct NewScreen NewScreen;
  132.  
  133.  
  134.    NewScreen.LeftEdge=0;
  135.    NewScreen.TopEdge=y;
  136.    NewScreen.Width=w;
  137.    NewScreen.Height=h;
  138.    NewScreen.Depth=d;
  139.    NewScreen.DetailPen=colour0;
  140.    NewScreen.BlockPen=colour1;
  141.    NewScreen.ViewModes=mode;
  142.    NewScreen.Type=CUSTOMSCREEN;
  143.    NewScreen.Font=NULL;
  144.    NewScreen.DefaultTitle=name;
  145.    NewScreen.Gadgets=NULL;
  146.    NewScreen.CustomBitMap=NULL;
  147.  
  148.    return (int)(OpenScreen(&NewScreen));
  149. }
  150.  
  151. /***********************************************************************/
  152.  
  153. int make_window(SHORT x, SHORT y, SHORT w, SHORT h,
  154. char *name, UBYTE colour0, UBYTE colour1,
  155. ULONG flags, ULONG iflags,
  156. struct Screen *screen,
  157. struct Gadget *gadg)
  158.  
  159. /* Initialize a NewWindow and Open it. */
  160.  
  161. {
  162.    struct NewWindow NewWindow;
  163.  
  164.    NewWindow.LeftEdge=x;
  165.    NewWindow.TopEdge=y;
  166.    NewWindow.Width=w;
  167.    NewWindow.Height=h;
  168.    NewWindow.DetailPen=colour0;
  169.    NewWindow.BlockPen=colour1;
  170.    NewWindow.Title=name;
  171.    NewWindow.Flags=flags;
  172.    NewWindow.IDCMPFlags=iflags;
  173.    NewWindow.Type=CUSTOMSCREEN;
  174.    NewWindow.FirstGadget=gadg;
  175.    NewWindow.CheckMark=NULL;
  176.    NewWindow.Screen=screen;
  177.    NewWindow.BitMap=NULL;
  178.    NewWindow.MinWidth=0;
  179.    NewWindow.MinHeight=0;
  180.    NewWindow.MaxWidth=0;
  181.    NewWindow.MaxHeight=0;
  182.  
  183.    return (int)(OpenWindow(&NewWindow));
  184.  
  185. }
  186.  
  187. /**************************************************************************/
  188.  
  189. void DrawFigure(struct RastPort *rp, short n, short p)
  190. {
  191.    SetAPen(rp,p);
  192.    Move(rp,n+20,270);
  193.    Draw(rp,n+0,250);
  194.    Draw(rp,n+0,220);
  195.    Draw(rp,n+20,200);
  196.    Draw(rp,n+40,220);
  197.    Draw(rp,n+40,250);
  198.    Draw(rp,n+20,270);
  199.    Draw(rp,n+20,300);
  200.    Draw(rp,n+40,320);
  201.    Draw(rp,n+20,340);
  202.    Move(rp,n+40,320);
  203.    Draw(rp,n+70,320);
  204.    Draw(rp,n+70,340);
  205.    Move(rp,n+70,320);
  206.    Draw(rp,n+90,300);
  207.  
  208.    Move(rp,n+20, 70);
  209.    Draw(rp,n+0,  50);
  210.    Draw(rp,n+0,  20);
  211.    Draw(rp,n+20, 00);
  212.    Draw(rp,n+40, 20);
  213.    Draw(rp,n+40, 50);
  214.    Draw(rp,n+20, 70);
  215.    Draw(rp,n+20,100);
  216.    Draw(rp,n+40,120);
  217.    Draw(rp,n+20,140);
  218.    Move(rp,n+40,120);
  219.    Draw(rp,n+70,120);
  220.    Draw(rp,n+70,140);
  221.    Move(rp,n+70,120);
  222.    Draw(rp,n+90,100);
  223. }
  224.